
/**
 * @file usermanagement/Home.js
 *  用户管理
 * @author
 */

import React, {Component,PropTypes} from 'react';
import {connect} from 'dva';
import {routerRedux,Link,withRouter } from 'dva/router';
import { Button,Modal,Row,Col,Table,Icon,Pagination,Card} from 'antd';

import Tabtitle from '../../../components/common/TabTitle';
import Search from '../../../components/common/Search';
import Adduser from './components/Adduser';
import DropOption  from '../../../components/common/DropOption';
import Tablelist from '../../../components/common/TableData';
import styles from './Home.less';


const Usermanagement = ({tag,dispatch, accoutuser,location:{
      pathname,
      query}}) => {
	if(accoutuser.status){
		const { userlist,page,} = accoutuser;
		const tag = 'accoutuser'
		// const handleMenuClick = (record, e) => {
		// if (e.key === '1'){
		// 	dispatch({
		// 		type: 'accoutuser/gotoEdit',
		// 		payload: {
		// 		  username:record.username,
		// 		}
		// 	})
		// } else if (e.key === '2') {
		// 		dispatch({
		// 			type: 'accoutuser/gotopermission',
		// 			payload: {
		// 				username:record.username,
		// 			}
		// 		})
		// 	}
		// }
		const columns = [
					{
						title: '用户名',
						dataIndex: 'username',
						key: 'username',
					},
					{
						title: '姓名',
						dataIndex: 'name',
						key: 'name',
					},
					{
						title: '联系方式',
						dataIndex: 'mobile',
						key: 'mobile',
					},
					{
						title: '邮箱',
						dataIndex: 'email',
						key: 'email',
					},
					{
						title: '创建时间',
						dataIndex: 'createDate',
						key: 'createDate',
					},

					{
						title: '操作',
						key: 'action',
						render: (text, record) => {
							function handleTopermission(){
								dispatch(routerRedux.push({
									pathname:'/systemMgt/accountMgt/userMgt/permission',
									query:{username:record.username}
								}))
							}
							function handleTorevise(){
								dispatch({
									type:'accoutuser/getRoles',
								})
								dispatch(routerRedux.push({
									pathname:'/systemMgt/accountMgt/userMgt/revise',
									query:{username:record.username}
								}))
							}
							return (
								<span>
									<span onClick={handleTorevise}><Link>编辑</Link></span>&nbsp;&nbsp;&nbsp;
									<span onClick={handleTopermission}><Link>设置权限</Link></span>
								</span>
							)
						},
					},
				];


			// 改变每页条数
			function onShowSizeChange(currentpage, pagesize) {
				if(location.href.indexOf('name')>-1){
					const pathArr = location.href.split('&')[1]
					const name = decodeURI(pathArr.split('=')[1])
					dispatch({
						type: 'accoutuser/getUsersList',
						payload: {
							currentpage:1,
							pagesize,
							name:name
						}
					})
				}else{
					dispatch({
						type: 'accoutuser/getUsersList',
						payload: {
							currentpage:1,
							pagesize,
						}
					})
				}
			}
			// 改变当前页
			function onChangecurrentpage(current,pagesize){
				if(location.href.indexOf('name')>-1){
					const pathArr = location.href.split('&')[1]
					const name = decodeURI(pathArr.split('=')[1])
					dispatch({
						type: 'accoutuser/getUsersList',
						payload: {
							currentpage:current,
							pagesize:pagesize,
							name:name
						}
					})
				}else{
					dispatch({
						type: 'accoutuser/getUsersList',
						payload: {
							currentpage:current,
							pagesize:pagesize,
						}
					})
				}
			}
			function alertAdduser(){
				dispatch({
					type:'accoutuser/getRoles',
				})
			}
			function onFormQuery(Values) {
				const name = Values.values;
				if(name){
					dispatch({
						type:'accoutuser/changeRoute',
						payload:{
							pagesize:15,
							currentpage:1,
							name:name.keyname || ''
						}
					})
				}else{
					if(name == undefined){
						dispatch({
						type:'accoutuser/changeRoute',
						payload:{
							pagesize:15,
							currentpage:1,
							name:''
						}
					})
					}
				}
			}
			return (
				<div>
					<Tabtitle value={'用户管理'}/>
						<div className="page-accoutuser-home content-inner">
							<div className={styles.searchcolumn}>
								<Row type="flex" justify="space-between" align="middle">
									<Col className={styles.searchbtn}>
										<Search
										buttonText="查询"
										onQuery={onFormQuery}
									/>
									</Col>
									<Col span={2} pull={2}>
										<a onClick={alertAdduser}>
											<Adduser className={styles.addbtn}/>
										</a>
									</Col>
								</Row>
							</div>
							<Card>
								<Tablelist
									columns={columns}
									data={userlist}
									page={page}
									onChangecurrentpage={onChangecurrentpage}
									record={record => record.username}
									onShowSizeChange={onShowSizeChange}
								/>
							</Card>
					</div>
				</div>
			)

	}else {
		return null
	}
}

Usermanagement.propTypes = {
}

export default connect(({accoutuser})=>({accoutuser}))(Usermanagement)
